package com.mate.cloud.lock;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * redisson 分布式锁实现
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/8/22 13:46
 * @updateUser: MI
 * @updateTime: 2025/8/22 13:46
 * @updateRemark: 修改内容
 * @version: 1.0
 */
public class LockTest {
    private static RedissonClient redissonClient;

    static {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.110.36:6379");
        config.useSingleServer().setPassword("seata@user123");
        redissonClient = Redisson.create(config);
    }

    public static void main(String[] args) throws InterruptedException {
        RLock rLock = redissonClient.getLock("updateAccount");
        // 最多等待100秒、上锁10s以后自动解锁
        if (rLock.tryLock(100, 10, TimeUnit.SECONDS)) {
            System.out.println("获取锁成功");
        }
        Thread.sleep(20000);
        rLock.unlock();

        redissonClient.shutdown();
    }
}
